﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection.Controller;
using Pixysoft.TestDrivens;
using System.Reflection;

namespace Pixysoft.Framework.Reflection.testdriven
{
    class Class_beanmap
    {

        public void test001()
        {
            //测试初始化

            IBeanMap map = ReflectionManager.CreateBeanMap(new SimpleObject());

            foreach (string key in map.Keys)
            {
                Console.Write(key + "::");
                Console.WriteLine(map.GetValue(key));
            }

            map.SetValue("Age", 1313);

            Console.WriteLine(map.GetBean<SimpleObject>().Age);
        }

        public void test002()
        {
            //全面测试 ObjectWithValueAndAttribute

            IBeanMap map = ReflectionManager.CreateBeanMap(new ObjectWithValueAndAttribute());

            foreach (string key in map.Keys)
            {
                Console.Write(key + "::");
                Console.WriteLine(map.GetValue(key));
                map.SetValue(key, null);
            }


            object result = map.GetBean();
            Assert.SaveOrVerify("test002", result);
        }

        public void test003()
        {
            //全面测试 ObjectWithValueAndAttribute

            IBeanMap map = ReflectionManager.CreateBeanMap(new ObjectWithAll());

            foreach (string key in map.Keys)
            {
                Console.Write(key + "::");
                Console.WriteLine(map.GetValue(key));
                map.SetValue(key, null);
            }


            object result = map.GetBean();
            Assert.SaveOrVerify("test003", result);
        }

        public void test_performance()
        {
            //------ Test started: Assembly: Pixysoft.Framework.Reflection.dll ------

            //reflect
            //    Time Elapsed:		2,703ms
            //    CPU time:		2,671,875,000ns
            //    Gen 0: 			39
            //    Gen 1: 			0
            //    Gen 2: 			0

            //dynamic
            //    Time Elapsed:		834ms
            //    CPU time:		843,750,000ns
            //    Gen 0: 			457
            //    Gen 1: 			0
            //    Gen 2: 			0

            //beanmap
            //    Time Elapsed:		861ms
            //    CPU time:		828,125,000ns
            //    Gen 0: 			205
            //    Gen 1: 			0
            //    Gen 2: 			0


            //1 passed, 0 failed, 0 skipped, took 4.52 seconds (Ad hoc).



            //测试性能

            SimpleObject obj = new SimpleObject();

            Pixysoft.Tools.CodeTimer.Initialize();

            Pixysoft.Tools.CodeTimer.Time("reflect", 100000, delegate()
            {
                foreach (PropertyInfo info in obj.GetType().GetProperties())
                {
                    object objs = info.GetValue(obj, null);
                    info.SetValue(obj, objs, null);
                }
            });

            Pixysoft.Tools.CodeTimer.Time("dynamic", 100000, delegate()
            {
                IDynamicType dynamic = ReflectionManager.CreateDynamicType(obj.GetType());

                foreach (IDynamicPropertyInfo info in dynamic.GetProperties())
                {
                    object objs = info.GetValue(obj, null);
                    info.SetValue(obj, objs, null);
                }
            });

            Pixysoft.Tools.CodeTimer.Time("beanmap", 100000, delegate()
            {
                IBeanMap bean = ReflectionManager.CreateBeanMap(obj);

                foreach (string key in bean.Keys)
                {
                    object objs = bean.GetValue(key);
                    bean.SetValue(key, objs);
                }

                obj = bean.GetBean<SimpleObject>();
            });
        }

        public void test_clone()
        {
            SimpleObject obj = new SimpleObject();
            obj.Age = 12;
            obj.Name = "hello";
            obj.Nage = null;

            foreach (PropertyInfo info in obj.GetType().GetProperties())
            {
                Console.WriteLine(info.PropertyType);

                object ovalue = null;
                Pixysoft.Tools.ParserHelper.TryParse(info.PropertyType, null,out ovalue, null);
                Pixysoft.Tools.ParserHelper.GetNullValue(info.PropertyType);
            }

            Console.WriteLine(ReflectionManager.CreateBeanMap(obj).Clone<SimpleObject>().Nage);

            IPojoValue iobj = ReflectionManager.CreatePojo<IPojoValue>();
            iobj.Name = "hellowrorld2";
            Console.WriteLine(ReflectionManager.CreateBeanMap(iobj).Clone<IPojoValue>().Name);
        }
    }

    class SimpleObject
    {
        string name = "helloworld";
        int age = 12;
        int? nage = null;
        SimpleObject2 obj = new SimpleObject2();

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public int? Nage
        {
            get
            {
                return this.nage;
            }
            set
            {
                this.nage = value;
            }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public SimpleObject2 Obj
        {
            get { return obj; }
            set { obj = value; }
        }
    }

    class SimpleObject2
    {
        string name = "helloworld";
        int age = 12;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}


